const db = uniCloud.database();

exports.main = async (event, context) => {
  const { data = {} } = event;
  const { page = 1, pageSize = 20, uid } = data;

  if (!uid) {
    return {
      code: 401,
      message: '未授权',
      data: null
    };
  }

  try {
    const skip = (Number(page) - 1) * Number(pageSize);
    
    // 查询该用户的收藏列表
    const result = await db.collection('food-favorites')
      .where({
        user_id: uid
      })
      .orderBy('create_date', 'desc')
      .skip(skip)
      .limit(Number(pageSize))
      .get();

    const { data: favorites, count } = result;

    return {
      code: 200,
      message: '成功',
      data: {
        list: favorites,
        total: count,
        totalPages: Math.ceil(count / Number(pageSize)),
        page: Number(page),
        pageSize: Number(pageSize)
      }
    };
  } catch (err) {
    return {
      code: 500,
      message: err.message,
      data: null
    };
  }
};
